package com.sunjob.mroresumeproject.partscontroller;

import com.sunjob.common.domain.mroresume.CKParts;
import com.sunjob.common.domain.mroresume.RPParts;
import com.sunjob.common.domain.mroresume.TFBus;
import com.sunjob.common.result.Result;
import com.sunjob.common.util.MROUtils;
import com.sunjob.mroresumeproject.service.CKPartsServcie;
import com.sunjob.mroresumeproject.service.RPPartsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/RPparts")
@CrossOrigin
@Api(tags = "部件维修履历")
public class RPPartsController {

    @Autowired
    RPPartsService rpPartsService;

    @RequestMapping("/getpages")
    public Result getpages(@RequestParam(value = "pages2")String pages2){

        int page2=Integer.parseInt(pages2);
        if (page2<1)
        {
            page2=1;

        }

        int size=5;
        //部件定检履历查出部件定检记录总条数
        int count=rpPartsService.querycount();
        //求总页数
        int pageCount = count %size == 0 ? count / size : count / size + 1;

        if(page2>pageCount)
        {
            page2=pageCount;
        }

        //第一页放五条数据
        int pages=(page2-1)*size;
        List<RPParts> list=rpPartsService.queryRPparts(pages,size);


        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);


        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;

    }



    //部件维修履历菜单查询
    @RequestMapping("/selectRPparts")
    @ApiOperation(value = "分页")
    public Result selectCKparts(){
        int page2=1;
        int size=5;

        //部件维修履历查出部件维修记录总条数
        int count=rpPartsService.querycount();
        //求总页数
        int pageCount = count %size == 0 ? count / size : count / size + 1;



        //第一页放五条数据
        int pages=(page2-1)*size;
        List<RPParts> list=rpPartsService.queryRPparts(pages,size);

        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);

        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;
    }

    //搜索框输入条件查询
    @RequestMapping("/selectRPcontent")
    @ApiOperation(value = "条件查询")
    public Result selectCKcontent(@ApiParam(value = "repairid",required = false) String repairid){
        //搜索框条件查询可以是部件维修编号或者部件名称
        List<RPParts> list=rpPartsService.queryRPparts2(repairid);

        //result返回查询出的部件维修履历记录
        Result result=new Result();
        result.setData(list);
        System.out.println(result);
        return result;


    }

    //新增按钮，部件定维修履历新增
    @RequestMapping("/insertRPparts")
    @ApiOperation(value = "新增")
    public void insertRPparts(RPParts rpParts){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime ldt = LocalDate.parse(rpParts.getStarttime2(),df).atStartOfDay();
        LocalDateTime ldt2 = LocalDate.parse(rpParts.getEndtime2(),df).atStartOfDay();
        if(rpParts.getFailreason()==""){
            rpParts.setFailreason("无");
            rpParts.setRPstate("完成");

        }
        else {

            rpParts.setRPstate("失败");


        }
        rpParts.setStarttime(ldt);
        rpParts.setEndtime(ldt2);
        rpParts.setStatus(1);
        rpParts.setRepairid(MROUtils.nextId());
        rpPartsService.insertRPparts(rpParts);
    }


    @RequestMapping("/insertRPparts2")
    public void insertTFBus(RPParts rpParts) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        Date date = sdf.parse(rpParts.getStarttime2().split("\"")[1]);
//        System.out.println(date);
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        rpParts.setStarttime(instant.atZone(zoneId).toLocalDateTime());

        if (rpParts.getFailreason()==""){
            rpParts.setFailreason("无");
            rpParts.setRPstate("完成");
        }
        else {
            rpParts.setRPstate("失败");
        }
        Date date1 = new Date();
        Instant instant1 = date1.toInstant();
        ZoneId zoneId1 = ZoneId.systemDefault();

        rpParts.setEndtime(instant1.atZone(zoneId1).toLocalDateTime());
        rpParts.setStatus(1);

        rpPartsService.insertRPparts(rpParts);
    }


    //编辑按钮，根据checkid查询出当前id的数据
    @RequestMapping("returnRPparts")
    @ApiOperation(value = "查询")
    public Result returnRPparts(@RequestParam("repairid")Long repairid){
        List<RPParts>rpParts=rpPartsService.queryRPpartsone(repairid);
        Result result=new Result();
        result.setData(rpParts);
        return result;
    }

    //对根据checkid查询出的部件维修履历数据做修改
    @RequestMapping("/updateRPparts")
    @ApiOperation(value = "修改")
    public Result updateCKparts(@RequestParam(value = "repairid",required = false)String repairid,
                                @RequestParam(value = "RPcontent",required = false)String RPcontent,
                                @RequestParam(value = "partstypes",required = false)String partstypes,
                                @RequestParam(value = "management",required = false)String management,
                                @RequestParam(value = "partsinitial",required = false)String partsinitial,
                                @RequestParam(value = "partsend",required = false)String partsend,
                                @RequestParam(value = "failreson",required = false)String  failreason,
                                @RequestParam(value = "money",required = false)String money,
                                @RequestParam(value = "starttime",required = false)String starttime,
                                @RequestParam(value = "endtime",required = false) String endtime
    ){

      rpPartsService.updateRPparts(repairid,RPcontent,partstypes,management,partsinitial,partsend,failreason,money,starttime,endtime);
        //返回修改后的数据列表
        List<RPParts>rpParts=rpPartsService.queryRPparts5();
        Result result=new Result();
        result.setData(rpParts);
        return result;
    }

    //根据repairid删除修改这条记录的状态
    @RequestMapping("/deleteRPparts")
    @ApiOperation(value = "删除")
    public Result deleteRPparts(@RequestParam("repairid")String repairid){
        //修改这条记录状态，0为消失，1为存在
        rpPartsService.deleteRPparts(repairid);

        //删除后查询部维修履历记录
        List<RPParts>rpParts=rpPartsService.queryRPparts5();
        Result result=new Result();
        result.setData(rpParts);
        return result;
    }

    @RequestMapping("/deleteRPparts2")
    public void deleteWHBus2(String repair2){
        System.out.println(repair2);
        String[] a= repair2.split(",");
        String[] c=  a[0].split("\\[");
        System.out.println(c[0]);
        System.out.println(c.length);
        String[] d = a[a.length-1].split("\\]");
        for (int i = 0; i < a.length; i++) {
            if (i==0){
                a[0]=c[1];

            }
            if (i==a.length-1){
                a[a.length-1]=d[0];
            }
           rpPartsService.deleteRPpartsone(a[i]);
        }
    }

}
